|
1
|
|
|
var AjaxSchedulerInit: NodeJS.Timer = null; |
|
2
|
|
|
var AjaxSchedulerRequests: Array<any> = []; |
|
3
|
|
|
var AjaxSchedulerRunning: Boolean = false; |
|
4
|
|
|
/** |
|
5
|
|
|
* AJAX MANAGER |
|
6
|
|
|
* @todo handle ajax request queue |
|
7
|
|
|
* @see https://bit.ly/2Tz0wrf |
|
8
|
|
|
*/ |
|
9
|
|
|
class ajaxScheduler { |
|
10
|
|
|
/** |
|
11
|
|
|
* Add ajax to queues |
|
12
|
|
|
* @param opt |
|
13
|
|
|
*/ |
|
14
|
|
|
static add(opt: JQueryAjaxSettings) { |
|
15
|
|
|
AjaxSchedulerRequests.push(opt); |
|
16
|
|
|
} |
|
17
|
|
|
/** |
|
18
|
|
|
* Remove ajax from queues |
|
19
|
|
|
* @param opt |
|
20
|
|
|
*/ |
|
21
|
|
|
static remove(opt: Object) { |
|
22
|
|
|
if (jQuery.inArray(opt, AjaxSchedulerRequests) > -1) { |
|
23
|
|
|
AjaxSchedulerRequests.splice( |
|
24
|
|
|
jQuery.inArray(opt, AjaxSchedulerRequests), |
|
25
|
|
|
1 |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
/** |
|
30
|
|
|
* Run Ajax Scheduler |
|
31
|
|
|
*/ |
|
32
|
|
|
static run() { |
|
33
|
|
|
var self = this; |
|
34
|
|
|
var oriSuc: () => void; |
|
35
|
|
|
//console.log(AjaxSchedulerRequests.length); |
|
36
|
|
|
if (AjaxSchedulerRequests.length > 0) { |
|
37
|
|
|
oriSuc = AjaxSchedulerRequests[0].complete; |
|
38
|
|
|
|
|
39
|
|
|
AjaxSchedulerRequests[0].complete = function () { |
|
40
|
|
|
if (typeof oriSuc === "function") { |
|
41
|
|
|
oriSuc(); |
|
42
|
|
|
} |
|
43
|
|
|
AjaxSchedulerRequests.shift(); |
|
44
|
|
|
self.run.apply(self, []); |
|
45
|
|
|
}; |
|
46
|
|
|
|
|
47
|
|
|
$.ajax(AjaxSchedulerRequests[0]); |
|
48
|
|
|
} else { |
|
49
|
|
|
AjaxSchedulerInit = setTimeout(function () { |
|
50
|
|
|
self.run.apply(self, []); |
|
51
|
|
|
}, 1000); |
|
52
|
|
|
} |
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
/** |
|
56
|
|
|
* Stop ajax scheduler |
|
57
|
|
|
*/ |
|
58
|
|
|
static stop() { |
|
59
|
|
|
AjaxSchedulerRequests = []; |
|
60
|
|
|
clearTimeout(AjaxSchedulerInit); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* RUN AJAX Scheduler |
|
66
|
|
|
* @param method POST, GET, HEAD, DELETE, OPTIONS, PATCH, PROPATCH |
|
67
|
|
|
* @description ajax request one by one |
|
68
|
|
|
* @todo scheduling any jquery ajax |
|
69
|
|
|
*/ |
|
70
|
|
|
function ajaxRun( |
|
71
|
|
|
url: string, |
|
72
|
|
|
method: string, |
|
73
|
|
|
data: object, |
|
74
|
|
|
success: Function, |
|
75
|
|
|
failed: Function, |
|
76
|
|
|
complete: Function |
|
77
|
|
|
) { |
|
78
|
|
|
if (!AjaxSchedulerRunning) { |
|
79
|
|
|
ajaxScheduler.run(); |
|
80
|
|
|
AjaxSchedulerRunning = true; |
|
81
|
|
|
} |
|
82
|
|
|
return ajaxScheduler.add({ |
|
83
|
|
|
url: url, |
|
84
|
|
|
method: method, |
|
85
|
|
|
timeout: 30000, // sets timeout to 30 seconds |
|
86
|
|
|
data: data, |
|
87
|
|
|
indicator: true, |
|
88
|
|
|
headers: { |
|
89
|
|
|
"unique-id": getUID(), |
|
90
|
|
|
Accept: "application/json", |
|
91
|
|
|
}, |
|
92
|
|
|
success: function (res) { |
|
93
|
|
|
if (typeof success == "function") { |
|
94
|
|
|
success(res); |
|
95
|
|
|
} else if (typeof success == "string") { |
|
96
|
|
|
___call(success, window, res); |
|
97
|
|
|
} else { |
|
98
|
|
|
console.log( |
|
99
|
|
|
success + " isnt success callback, instead of " + typeof success |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
}, |
|
103
|
|
|
error: function (err: JQueryXHR) { |
|
104
|
|
|
if (typeof failed == "function") { |
|
105
|
|
|
failed(err); |
|
106
|
|
|
} |
|
107
|
|
|
}, |
|
108
|
|
|
complete: function (res: JQueryXHR) { |
|
109
|
|
|
AJAX = null; |
|
110
|
|
|
if (typeof complete == "function") { |
|
111
|
|
|
complete(res); |
|
112
|
|
|
} |
|
113
|
|
|
//gexec('Ajax_Reload'); |
|
114
|
|
|
}, |
|
115
|
|
|
}); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
function ajaxFormSchedule() { |
|
119
|
|
|
$(document).on("submit", "form", function (e) { |
|
120
|
|
|
e.preventDefault(); |
|
121
|
|
|
var t = $(this); |
|
122
|
|
|
var s = t.data("success"), |
|
123
|
|
|
err = t.data("error"), |
|
124
|
|
|
c = t.data("complete"); |
|
125
|
|
|
ajaxScheduler.add({ |
|
126
|
|
|
url: t.attr("action"), |
|
127
|
|
|
method: t.attr("method"), |
|
128
|
|
|
data: t.serialize(), |
|
129
|
|
|
success: s, |
|
130
|
|
|
error: err, |
|
131
|
|
|
complete: c, |
|
132
|
|
|
}); |
|
133
|
|
|
}); |
|
134
|
|
|
} |
|
135
|
|
|
|